package com.lyw.i03lock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * 可重复锁， 可以重复使用
 *
 * @author lyw
 * @Date 2023/7/9-上午12:26
 */

class RLSale {

  private final ReentrantLock lock = new ReentrantLock();
  private int num = 30;
  public void sale() {
    lock.lock();
    try {
      if (num > 0) {
        System.out.println(Thread.currentThread().getName() + "卖出了一张票，还有 " + --num + " 张");
      }
    } finally {
      lock.unlock();
    }
  }
}
public class ReentrantLockDemo {

  public static void main(String[] args) {
    RLSale rlSale = new RLSale();
    for (int i = 0; i < 3; i++) {
      new Thread(() -> {
        for (int j = 0; j < 40; j++) {
          rlSale.sale();
        }
      }, "线程 " + i).start();
    }
    int a = a();
    System.out.println(a());
  }

  public static int a() {

    int i = 0;
    try {
      i++;
      System.exit(0);
      return i;
    } finally {
      i++;
      return i;
    }
  }
}
